import pygame.image
import pygame.image

from game import SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_HALF_WIDTH, SCREEN_HALF_HEIGHT, FULL_HEIGHT, FULL_WIDTH, \
    ScenePassStatus

"""
场景基类
"""
class BaseScene:
    def __init__(self, xiao_tie, background_path):
        """
        参数初始化
        :param xiao_tie: 小铁类
        :param background_path: 背景图片地址
        """
        self.font = pygame.font.Font("resources/font/迷你简粗宋.TTF", 15)
        self.xiao_tie = xiao_tie
        self.path = background_path
        self.background_image = pygame.image.load(background_path)
        self.current_surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT)).convert()

        if (self.background_image.get_width() <= SCREEN_WIDTH) and \
                (self.background_image.get_height() <= SCREEN_HEIGHT):
            self.sub_scene_flag = False
        else:
            self.sub_scene_flag = True
        # 默认为正常模式
        self.pass_status = ScenePassStatus.normal

    def __boundary(self):
        """
        分划界限
        :return:
        """
        pos_x = self.xiao_tie.pos_x
        pos_y = self.xiao_tie.pos_y
        if pos_x < SCREEN_HALF_WIDTH:
            view_x = 0
        elif pos_x > FULL_WIDTH - SCREEN_HALF_WIDTH - 1:
            view_x = FULL_WIDTH - SCREEN_WIDTH - 1
        else:
            view_x = pos_x - SCREEN_HALF_WIDTH

        if pos_y < SCREEN_HALF_HEIGHT:
            view_y = 0
        elif pos_y > FULL_HEIGHT - SCREEN_HALF_HEIGHT - 1:
            view_y = FULL_HEIGHT - SCREEN_HEIGHT - 1
        else:
            view_y = pos_y - SCREEN_HALF_HEIGHT

        return view_x, view_y

    def get_current_surface(self):
        """
        获取当前窗口类
        :return:
        """
        if not self.sub_scene_flag:
            self.current_surface.blit(self.background_image, self.current_surface.get_rect())
        else:
            self.view_x, self.view_y = self.__boundary()
            image = self.background_image.subsurface((self.view_x, self.view_y, SCREEN_WIDTH, SCREEN_HEIGHT))
            self.current_surface.blit(image, self.current_surface.get_rect())
        self.draw_actor()
        return self.current_surface

    def draw_actor(self):
        """
        绘制角色
        :return:
        """
        pass

    def run(self, key_down_flag, key_list):
        """
        实现人物移动
        :param key_down_flag:
        :param key_list:
        :return:
        """
        pass

    def init_groups(self):
        """
        初始化组别
        :return:
        """
        pass

        # 获取场景通关的状态
    def get_pass_status(self):
        """
        获取通关的状态
        :return: 状态，下一个场景枚举
        """
        return self.pass_status, None